package com.bw.gmall.api.repository;

import com.bw.gmall.api.model.KeywordStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class KeywordStatsRepository {
/*
* KeywordStatsRepository：关键词统计数据访问类，提供查询关键词 TopN 数据和指定时间范围内数据的方法，
* 操作表dws_keyword_topn_window。
* */
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<KeywordStats> findTopN(int limit) {
        String sql = "SELECT * FROM dws_keyword_topn_window ORDER BY rank ASC LIMIT " + limit;
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeywordStats.class));
    }

    public List<KeywordStats> findByTimeRange(String startTime, String endTime) {
        String sql = "SELECT * FROM dws_keyword_topn_window WHERE stats_time BETWEEN ? AND ? ORDER BY rank ASC";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeywordStats.class), startTime, endTime);
    }
}